Title Banner

Previous Book Contents Book Index Next

Inside Macintosh: QuickDraw GX Graphics /
Chapter 2 - Geometric Shapes / Using Geometric Shapes


Converting Between Geometric Shape Types

QuickDraw GX provides the GXGetShapeType and GXSetShapeType functions to allow you to manipulate a shape's type. The GXGetShapeType function simply returns the value of the shape type property for a specified shape. For geometric shapes, the possible values returned from this function are

The GXSetShapeType function allows you to change the shape type of an existing shape. In doing so, this function often has to reinterpret the geometry of the shape. This reinterpretation is called type conversion. Sometimes the conversion makes sense and doesn't lose any data. For example, you might want to convert a line shape to a polygon shape so that you can add more contours to the shape. Some conversions, however, aren't as useful and data can be lost. For example, converting a complex path shape to a point shape can result in the loss of a significant amount of data.

In general, when converting between geometric shape types, QuickDraw GX exhibits different behavior in these four situations:

When converting to an empty shape or a full shape, all the information in the original shape's geometry is lost--the result is simple an empty shape or a full shape, respectively.

The following three subsections discuss the other cases in more detail.

Converting Shapes to Points, Lines, and Rectangles

When converting a shape to a point, line, or rectangle, QuickDraw GX uses the bounding rectangle of the original shape. (Bounding rectangles are defined in the chapter "Geometric Operations" in this book. For an example, see Figure 2-41 on page 2-68.) QuickDraw GX uses the bounding rectangle differently, depending on which shape type you are converting to:

Listing 2-19 creates a path shape, which is converted subsequently to a rectangle shape, then to a line shape, and finally to a point shape.

Listing 2-19 Creating a figure-eight path shape

void CreateFigureEight(void)
{
   gxShape     aPathShape;
   
   static long figureEightGeometry[] = {1,/* number of contours */
                                        4, /* number of points */
                                        0xF0000000, /* 1111 ... */
                                        ff(50), ff(50),  /* off */
                                        ff(200),ff(200), /* off */
                                        ff(50), ff(200), /* off */
                                        ff(200),ff(50)}; /* off */
      
   aPathShape = GXNewPaths((gxPaths *) figureEightGeometry);
   GXSetShapeFill(aPathShape, gxClosedFrameFill);

   GXDrawShape(aPathShape);
}
The resulting path geometry is shown in Figure 2-40.

Figure 2-40 A figure-eight path shape

If you convert this shape to a rectangle shape, using the call

GXSetShapeType(aPathShape, gxRectangleType);
the resulting shape is the bounding rectangle for the original path shape, as shown in Figure 2-41.

Figure 2-41 A path shape before and after conversion to a rectangle shape

If you convert the original path shape to a line shape, using the call

GXSetShapeType(aPathShape, gxLineType);
the resulting shape is a diagonal line from the upper-left corner of the path's bounding rectangle to its lower-right corner, as shown in Figure 2-42.

Figure 2-42 A path shape before and after conversion to a line shape

Finally, if you convert the orignal path shape to a point shape, using the call

GXSetShapeType(aPathShape, gxPointType);
the resulting shape is the point at the upper-left corner of the path's bounding rectangle, as shown in Figure 2-43.

Figure 2-43 A path shape before and after conversion to a point shape

The next two sections give examples of converting to the curve shape type and of converting to the polygon and path shape types.

For more information about the GXSetShapeType function, see the chapter "Shape Objects" of Inside Macintosh: QuickDraw GX Objects.

Converting Shapes to Curve Shapes

When converting other geometric shapes to a curve shape, QuickDraw GX takes one of these approaches:

The sample function in Listing 2-20 creates a line shape and converts it to a curve.

Listing 2-20 Converting a line to a curve

void ConvertLineToCurve(void)
{
   gxShape       aLineShape;
   
   static gxLine diagonalGeometry = {ff(50), ff(50),
                                     ff(150), ff(150)};

      
   aLineShape = GXNewLine(&diagonalGeometry);
   GXSetShapeType(aLineShape, gxCurveType);
   
   GXDrawShape(aLineShape);
   
   GXDisposeShape(aLineShape);
}
The original line shape and the resulting curve shape are shown in Figure 2-44.

Figure 2-44 A line shape before and after conversion to a curve shape

Notice that the converted curve looks just like the original line. The only difference between the two shapes is that the curve shape has an additional off-curve control point, which is set to be identical to the last point.

The sample function in Listing 2-21 creates a rectangle shape and converts it to a curve shape.

Listing 2-21 Converting a rectangle to a curve

void ConvertRectangleToCurve(void)
{
   gxShape aRectangleShape;
   
   static gxRectangle  rectangleGeometry = {ff(50), ff(50),
                                            ff(150), ff(150)};

      
   aRectangleShape = GXNewRectangle(&rectangleGeometry);
   GXSetShapeType(aRectangleShape, gxCurveType);

   GXDrawShape(aRectangleShape);
   
   GXDisposeShape(aLineShape);
}
The original rectangle and the resulting curve are both shown in Figure 2-45.

Figure 2-45 A rectangle shape before and after conversion to a curve shape

As in the previous example, the off-curve control point of the curve shape is set to be the same as the last point, which results in the curve shape being a straight line.

The next example, shown in Listing 2-22, shows how QuickDraw GX converts a polygon shape to a curve shape.

Listing 2-22 Converting a polygon shape to a curve shape

void ConvertPolygonToCurve(void)
{
   gxShape aPolygonShape;
   
   static long aPolygonGeometry[] = {1, /* number of contours */
                                     4, /* number of points */
                                     ff(50), ff(50),   
                                     ff(150), ff(50), 
                                     ff(150), ff(150), 
                                     ff(50), ff(150)};

      
   aPolygonShape = GXNewPolygons((gxPolygons *) aPolygonGeometry);
   GXSetShapeType(aPolygonShape, gxCurveType);

   GXDrawShape(aPolygonShape);

   GXDisposeShape(aPolygonShape);
}
In this example, QuickDraw GX sets the three geometric points of the resulting curve to be the first three geometric points of the original polygon. (Converting from path shapes to curve shapes works in the same way.) The original polygon and the resulting curve are shown in Figure 2-46.

Figure 2-46 A polygon shape before and after conversion to a curve shape

Notice that even though the polygon in this example looks the same as the rectangle in Figure 2-45, the converted curve shape looks quite different.

The next section gives examples of converting shapes to polygon and path shapes.

For more information about the GXSetShapeType function in general, see the chapter "Shape Objects" of Inside Macintosh: QuickDraw GX Objects.

Converting Shapes to Polygons and Paths

When converting other geometric shapes to polygon or path shapes, the original shapes don't lose any geometric information. For example, when you convert a line shape to a path shape, the resulting path shape contains one contour with two geometric points, both on curve--an exact duplicate of the original line.

You can even convert a curve shape to a polygon shape without losing geometric information, although the result does draw differently. The resulting polygon has one contour and three geometric points--the same three geometric points as the original curve. If you convert the polygon back to a curve, you end up with the original curve.

When you convert a rectangle shape to a polygon shape, as shown in Listing 2-23, the original shape and the resulting shape look exactly the same.

Listing 2-23 Converting a rectangle shape to a polygon shape

void ConvertRectangleToPolygon(void)
{
   gxShape aRectangleShape;
   
   static gxRectangle  rectangleGeometry = {ff(50), ff(50),
                                            ff(150), ff(150)};

      
   aRectangleShape = GXNewRectangle(&rectangleGeometry);
   GXSetShapeType(aRectangleShape, gxPolygonType);

   GXDrawShape(aRectangleShape);

   GXDisposeShape(aRectangleShape);
}
The original rectangle and the resulting polygon are shown in Figure 2-47.

Figure 2-47 A rectangle shape before and after conversion to a polygon shape

Converting from a path shape to a polygon shape, however, does lose geometric information. The resulting polygon contains the same geometric points as the original path; however, the points are all considered on-curve points. The original information about which points were on curve and which points were off curve is lost during this conversion.

As an example, Listing 2-24 creates a path shape and converts it to a polygon shape.

Listing 2-24 Converting a path shape to a polygon shape

void ConvertPathToPolygon(void)
{
   gxShape  aPathShape;
   
   static long figureEightGeometry[] = {1, /* # of contours */
                                        4, /* # of points */
                                        0xF0000000, /* 1111 ... */
                                        ff(50), ff(50),  /* off */
                                        ff(200),ff(200), /* off */
                                        ff(50), ff(200), /* off */
                                        ff(200),ff(50)}; /* off */

      
   aPathShape = GXNewPaths((gxPaths *) figureEightGeometry);
   GXSetShapeFill(aPathShape, gxHollowFill);
   GXSetShapeType(aPathShape, gxPolygonType);

   GXDrawShape(aPathShape);

   GXDisposeShape(aPathShape);
}
Figure 2-48 shows the original path shape and the resulting polygon shape.

Figure 2-48 A path shape before and after conversion to a polygon shape

Note
You can request that QuickDraw GX calculate a better polygon approximation to a path by setting the curve error property of the path shape's style object before calling the GXSetShapeType function. See the next chapter, "Geometric Styles," for examples.
Converting in the other direction, however--from a polygon shape to a path shape--retains all of the geometry information and the resulting path shape looks exactly the same as the original polygon shape. The sample function in Listing 2-25 gives an example.

Listing 2-25 Converting a polygon shape to a path shape

void ConvertPolygonToPath(void)
{
   gxShape     aPolygonShape;
   
   static long aPolygonGeometry[] = {2, /* number of contours */
                                     3, /* number of points */
                                     ff(50), ff(50),   
                                     ff(100), ff(80), 
                                     ff(50), ff(110),
                                     3, /* number of points */
                                     ff(200), ff(50),   
                                     ff(150), ff(80), 
                                     ff(200), ff(110)};
                                 
   
   aPolygonShape = GXNewPolygons((gxPolygons *)
                                  aPolygonGeometry);
   GXSetShapeType(aPolygonShape, gxPathType);
   
   GXDrawShape(aPolygonShape);
   
   GXDisposeShape(aPolygonShape);
}
The original polygon shape and the converted path shape are shown in Figure 2-49.

Figure 2-49 Polygon shape with two contours before and after conversion to a path shape

For more information about the GXSetShapeType function, see the chapter "Shape Objects" of Inside Macintosh: QuickDraw GX Objects.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
7 JUL 1996




Navigation graphic, see text links

Main | Page One | What's New | Apple Computer, Inc. | Find It | Contact Us | Help